package com.yixun.component;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.*;
import redis.clients.jedis.exceptions.JedisException;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * desc:操作jedis工具类
 * author:happy-sc
 * date:2016/9/23.
 */
@Component
@Slf4j
public class RedisService {

    @Autowired
    private JedisPool jedisPool;

    /**
     * 向集合添加一个或多个成员
     *
     * @param key
     * @param fields
     * @return
     */
    public Long sadd(String key, String... fields) {
        Jedis jedis = getJedis();
        try {
            return jedis.sadd(key, fields);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }


    /**
     * 返回给定所有集合的差集
     *
     * @param key1
     * @param key2
     * @return
     */
    public Set sDiff(String key1, String key2) {
        Jedis jedis = getJedis();
        try {

            return jedis.sdiff(key1, key2);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 删除
     *
     *
     * @param key
     * @return
     */
    public Long srem(String key) {
        Jedis jedis = getJedis();
        try {

            return jedis.srem(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }


    public Long setnx(String key1, String key2) {
        Jedis jedis = getJedis();
        try {

            return jedis.setnx(key1, key2);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 返回给定所有集合的差集并存储在 dstkey 中
     *
     * @param dstkey
     * @param key
     * @return
     */
    public Long sDiffStore(String dstkey, String... key) {
        Jedis jedis = getJedis();
        try {
            return jedis.sdiffstore(dstkey, key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 返回给定所有集合的差集并存储在 dstkey 中
     *
     * @param
     * @param key
     * @return
     */
    public Map<String, String> hgetAll(String key) {
        Jedis jedis = getJedis();
        try {
            return jedis.hgetAll(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }


    /**
     * 获取过期时间
     *
     * @param key
     * @return
     */
    public Long ttl(String key) {
        Jedis jedis = getJedis();
        try {
            return jedis.ttl(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }

    /**
     * 增加数值
     *
     * @param key
     * @return
     */
    public Long incrBy(String key, Long increment) {
        Jedis jedis = getJedis();
        try {
            return jedis.incrBy(key, increment);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }

    /**
     * 增加数值
     *
     * @param key
     * @return
     */
    public Double zincrby(String key, double increment, String mobile) {
        Jedis jedis = getJedis();
        try {
            return jedis.zincrby(key, increment, mobile);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }

    /**
     * 减少数值
     *
     * @param key
     * @return
     */
    public Long decrBy(String key, Long increment) {
        Jedis jedis = getJedis();
        try {
            return jedis.decrBy(key, increment);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }

    /**
     * 减少数值
     *
     * @param key
     * @return
     */
    public Boolean sismember(String key, String value) {
        Jedis jedis = getJedis();
        try {
            return jedis.sismember(key, value);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }

    /**
     * 取交集
     * @param key
     * @return
     */
    public Set<String> sinter(String ...key) {
        Jedis jedis = getJedis();
        try {
            return jedis.sinter(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }
    public Set<String> smembers(String key) {
        Jedis jedis = getJedis();
        try {
            return jedis.smembers(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }
    /**
     * @return
     */
    public String watch(String... key) {
        Jedis jedis = getJedis();
        try {
            return jedis.watch(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * @return
     */
    public Transaction multi() {
        Jedis jedis = getJedis();
        try {
            return jedis.multi();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }


    /**
     * 减1数值
     *
     * @param key
     * @return
     */
    public Long decr(String key) {
        Jedis jedis = getJedis();
        try {
            return jedis.decr(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }


    /**
     * 删除一个哈希表字段
     *
     * @param key
     * @param field
     * @return
     */
    public Long hDel(String key, String field) {
        Jedis jedis = getJedis();
        try {
            return jedis.hdel(key, field);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 将哈希表 key 中的字段 field 的值设为 value
     *
     * @param key
     * @param field
     * @param value
     * @return
     */
    public Long hSet(String key, String field, String value) {
        Jedis jedis = getJedis();
        try {

            return jedis.hset(key, field, value);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 将哈希表 key 中的字段 field 的值设为 value
     *
     * @param key
     * @param field
     * @param value
     * @return
     */
    public Long hSet(byte[] key, byte[] field, byte[] value) {
        Jedis jedis = getJedis();
        try {

            return jedis.hset(key, field, value);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 获取哈希表中字段的数量
     *
     * @param key
     * @return
     */
    public Long hLen(String key) {
        Jedis jedis = getJedis();
        try {
            return jedis.hlen(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 同时将多个 field-value (域-值)对设置到哈希表 key 中
     *
     * @param key   哈希表 key
     * @param value field-value (域-值)
     * @return
     */
    public String hMset(String key, Map value) {
        Jedis jedis = getJedis();
        try {
            return jedis.hmset(key, value);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 获取存储在哈希表中指定字段的值
     *
     * @param key
     * @param filed
     * @return
     */
    public String hGet(String key, String filed) {
        Jedis jedis = getJedis();
        try {
            return jedis.hget(key, filed);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }
    public byte[] hGet(byte[] key, byte[] filed) {
        Jedis jedis = getJedis();
        try {
            return jedis.hget(key, filed);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }


    /**
     * 获取在哈希表中指定 key 的所有字段和值
     *
     * @param key
     * @return
     */
    public Map<String, String> hGetAll(String key) {
        Jedis jedis = getJedis();
        try {
            return jedis.hgetAll(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }


    /**
     * 为给定 key 设置过期时间
     *
     * @param key
     * @param seconds
     * @return
     */
    public Long expire(String key, int seconds) {
        Jedis jedis = getJedis();
        try {
            return jedis.expire(key, seconds);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 设置指定 key 的值
     *
     * @param key
     * @param value
     * @return
     */
    public String set(String key, String value) {
        Jedis jedis = getJedis();
        try {
            return jedis.set(key, value);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 设置指定 key 的值
     *
     * @param key
     * @param value
     * @return
     */
    public String set(String key, String value, Integer index) {
        Jedis jedis = getJedis();
        try {
            jedis.select(index);
            return jedis.set(key, value);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 将值 value 关联到 key ，并将 key 的过期时间设为 seconds (以秒为单位)
     *
     * @param key
     * @param seconds
     * @param value
     * @return
     */
    public String setex(byte[] key, int seconds, byte[] value) {
        Jedis jedis = getJedis();
        try {
            return jedis.setex(key, seconds, value);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 将值 value 关联到 key ，并将 key 的过期时间设为 seconds (以秒为单位)
     *
     * @param key
     * @param seconds
     * @param value
     * @return
     */
    public String setex(String key, int seconds, String value) {
        Jedis jedis = getJedis();
        try {
            return jedis.setex(key, seconds, value);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    public String setex(String key,Integer second, String value,Integer index){
        Jedis jedis = getJedis();
        try {
            jedis.select(index);
            return jedis.setex(key,second,value);
        }catch (Exception e){
            log.error(e.getMessage(), e);
        }finally {
            returnResource(jedis);
        }
        return null;
    }

    public String get(String key,Integer index){
        Jedis jedis = getJedis();
        try {
            jedis.select(index);
            return jedis.get(key);
        }catch (Exception e){
            log.error(e.getMessage(), e);
        }finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 获取指定 key 的值
     *
     * @param key
     * @return
     */
    public byte[] get(byte[] key) {
        Jedis jedis = getJedis();
        try {
            return jedis.get(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 获取指定 key 的值
     *
     * @param key
     * @return
     */
    public String get(String key) {
        Jedis jedis = getJedis();
        try {
            return jedis.get(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 将给定 key 的值设为 value ，并返回 key 的旧值(old value)
     *
     * @param key
     * @return
     */
    public String getSet(String key, String value) {
        Jedis jedis = getJedis();
        try {
            return jedis.getSet(key, value);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 该命令用于在 key 存在时删除 key
     *
     * @param key
     * @return
     */
    public Long del(String key) {
        Jedis jedis = getJedis();
        try {
            return jedis.del(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }
    public Long delByIndex(String key,Integer index){
        Jedis jedis = getJedis();
        try {
            jedis.select(index);
            return jedis.del(key);
        }catch (Exception e){
            log.error("delByIndex：key:{},index:{}",key,index);
        }finally {
            returnResource(jedis);
        }
        return null;
    }

    /***
     *根据前缀批量删除key
     * @param key  前缀
     * @param index  索引库
     */
    public Long batchDel(String key,Integer index){
        Jedis jedis = getJedis();
        jedis.select(index);
        // 游标初始值为0
        String cursor = ScanParams.SCAN_POINTER_START;
        ScanParams scanParams = new ScanParams();
        // 匹配
        scanParams.match(key);
        scanParams.count(Integer.MAX_VALUE);
        try {
            while (true){
                //使用scan命令获取数据，使用cursor游标记录位置，下次循环使用
                ScanResult<String> scanResult = jedis.scan(cursor, scanParams);
                // 返回0 说明遍历完成
                cursor = scanResult.getStringCursor();
                List<String> list = scanResult.getResult();
                for(int m = 0;m < list.size();m++){
                    String mapentry = list.get(m);
                    jedis.del(key, mapentry);
                }
                if ("0".equals(cursor)){
                    break;
                }
            }
        }catch (Exception e){
            log.info("批量删除报错：{}",key);
            e.printStackTrace();
        }finally {
            returnResource(jedis);
        }
        return null;
    }



    /**
     * 检查给定 key 是否存在
     *
     * @param key
     * @return
     */
    public Boolean exists(String key) {
        Jedis jedis = getJedis();
        try {
            return jedis.exists(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return false;
    }

    public Boolean exists(String key,Integer index) {
        Jedis jedis = getJedis();
        try {
            jedis.select(index);
            return jedis.exists(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return false;
    }


    /**
     * 向list插入byte[]数据
     *
     * @param key
     * @param value
     * @return
     */
    public Long rpush(byte[] key, byte[] value) {
        Jedis jedis = getJedis();
        try {
            return jedis.rpush(key, value);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }

    /**
     * 向list插入String类型的数据
     *
     * @param key
     * @param value
     * @return
     */
    public Long rpush(String key, String... value) {
        Jedis jedis = getJedis();
        try {
            return jedis.rpush(key, value);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 获取列表指定位置的值
     *
     * @param key
     * @param start
     * @param end
     * @return
     */
    public List<byte[]> lrange(byte[] key, long start, long end) {
        Jedis jedis = getJedis();
        try {
            return jedis.lrange(key, start, end);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }

    /**
     * 获取列表指定位置的值
     *
     * @param key
     * @param start
     * @param end
     * @return
     */
    public List<String> lrange(String key, long start, long end) {
        Jedis jedis = getJedis();
        try {
            return jedis.lrange(key, start, end);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }


    /**
     * 弹出list头部元素
     *
     * @param key
     * @return
     */
    public String lpop(String key) {
        Jedis jedis = getJedis();
        try {
            return jedis.lpop(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 对list元素进行裁剪
     *
     * @param key
     * @return
     */
    public String ltrim(String key, long start, long end) {
        Jedis jedis = getJedis();
        try {
            return jedis.ltrim(key, start, end);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * llen
     *
     * @param key
     * @return
     */
    public Long llen(String key) {
        Jedis jedis = getJedis();
        try {

            return jedis.llen(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 自增ID
     *
     * @param key
     * @return
     */
    public Long incr(String key) {
        Jedis jedis = getJedis();
        try {
            return jedis.incr(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 获取列表指定位置的值
     *
     * @param key
     * @param index
     * @return
     */
    public String lindex(String key, long index) {
        Jedis jedis = getJedis();
        try {
            return jedis.lindex(key, index);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }

    /**
     * 向有序集合插入
     *
     * @param key
     * @param score
     * @param obj
     * @return
     */
    public Long zadd(byte[] key, double score, byte[] obj) {
        Jedis jedis = getJedis();
        try {
            return jedis.zadd(key, score, obj);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }

    /**
     * 向有序集合插入
     *
     * @param key
     * @param score
     * @param obj
     * @return
     */
    public Long zadd(String key, double score, String obj) {
        Jedis jedis = getJedis();
        try {
            return jedis.zadd(key, score, obj);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }

    /**
     * 获取set里面的值
     *
     * @param key
     * @param start
     * @param end
     * @return
     */
    public Set<byte[]> zrange(byte[] key, long start, long end) {
        Jedis jedis = getJedis();
        try {
            return jedis.zrange(key, start, end);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }

    /**
     * 获取set里面的值
     *
     * @param key
     * @param start
     * @param end
     * @return
     */
    public Set<byte[]> zrangeByScore(byte[] key, double start, double end) {
        Jedis jedis = getJedis();
        try {
            return jedis.zrangeByScore(key, start, end);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }

    /**
     * 获取set里面的值
     *
     * @param key
     * @param start
     * @param end
     * @return
     */
    public Set<String> zrange(String key, long start, long end) {
        Jedis jedis = getJedis();
        try {
            return jedis.zrange(key, start, end);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }

    /**
     * 获取set里面的值
     *
     * @param key
     * @param start
     * @param end
     * @return
     */
    public Set<String> zrangeByScore(String key, double start, double end) {
        Jedis jedis = getJedis();
        try {
            return jedis.zrangeByScore(key, start, end);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }

    /**
     * 获取set里面的值
     *
     * @param key
     * @param member
     * @return
     */
    public Long zrank(String key, String member) {
        Jedis jedis = getJedis();
        try {

            return jedis.zrank(key, member);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }

    /**
     * 删除一个或者多个元素
     *
     * @param key
     * @param members
     * @return
     */
    public Long zrem(byte[] key, byte[]... members) {
        Jedis jedis = getJedis();
        try {
            return jedis.zrem(key, members);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }

    /**
     * 删除一个或者多个元素
     *
     * @param key
     * @param members
     * @return
     */
    public Long zrem(String key, String... members) {
        Jedis jedis = getJedis();
        try {
            return jedis.zrem(key, members);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }

    /**
     * 按照分值 删除指定数据
     *
     * @param key
     * @param start
     * @param end
     * @return
     */
    public Long zremrangeByScore(byte[] key, byte[] start, byte[] end) {
        Jedis jedis = getJedis();
        try {
            return jedis.zremrangeByScore(key, start, end);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }

    /**
     * 按照分值 删除指定数据
     *
     * @param key
     * @param start
     * @param end
     * @return
     */
    public Long zremrangeByScore(String key, long start, long end) {
        Jedis jedis = getJedis();
        try {
            return jedis.zremrangeByScore(key, start, end);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }

    /**
     * 返回有序集中，成员的分数值
     *
     * @param key
     * @param member
     * @return
     */
    public Double zscore(byte[] key, byte[] member) {
        Jedis jedis = getJedis();
        try {
            return jedis.zscore(key, member);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * list  rpoplpush
     *
     * @param
     * @param
     * @return
     */
    public String rpoplpush(String key1, String key2) {
        Jedis jedis = getJedis();
        try {
            return jedis.rpoplpush(key1, key2);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * list  rpoplpush
     *
     * @param key
     * @param member
     * @return
     */
    public Long lpush(String key, String member) {
        Jedis jedis = getJedis();
        try {
            return jedis.lpush(key, member);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * list lrem
     *
     * @param key
     * @param member
     * @return
     */
    public Long lrem(String key, String member) {
        Jedis jedis = getJedis();
        try {
            return jedis.lrem(key, 0, member);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 返回有序集中，成员的分数值
     *
     * @param key
     * @param member
     * @return
     */
    public Double zscore(String key, String member) {
        Jedis jedis = getJedis();
        try {
            return jedis.zscore(key, member);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;
    }


    public Long zcard(byte[] key) {
        Jedis jedis = getJedis();
        try {
            return jedis.zcard(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }

    public Long zcard(String key) {
        Jedis jedis = getJedis();
        try {
            return jedis.zcard(key);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }

    public Set<byte[]> zrevrange(byte[] key, long start, long end) {
        Jedis jedis = getJedis();
        try {
            return jedis.zrevrange(key, start, end);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            returnResource(jedis);
        }
        return null;

    }


    public void returnResource(Jedis jedis) {
        jedis.close();
    }


    protected Jedis getJedis() throws JedisException {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
        } catch (JedisException e) {

            if (jedis != null) {
                jedis.close();
            }
            throw e;
        }
        return jedis;
    }

}

